// pages/details/details.js
const db = wx.cloud.database()
const app = getApp();

const goods_col = db.collection('goods');
const hotDatas_col = db.collection('hotDatas');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        newsId: '',   //页面传参的id
        signWord: '',  //页面传参的sign
        oneDetail: [],  //单个数据
        showShare: false,
        shownoShare: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 状态栏样式
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#ffffff;',
        });
        // 获取到index页面的传值id; 获取状态栏信息
        this.setData({
            newsId: options.id,
            signWord: options.sign,
            navHeight: app.globalData.navHeight,
            navTop: app.globalData.navTop,
            ztlHeight: app.globalData.statusBarHeight,
        });

        this.getOneData();
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let signId = app.globalData.userId;
        let newCode = app.globalData.code;
        if(signId !== "" && newCode !== "") {
            wx.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
            });
            this.setData({
                showShare: true,
                shownoShare: false,
            });
        }else{
            wx.hideShareMenu({
                menus: ['shareAppMessage', 'shareTimeline']
            }); 
        }
    },

    // 通过id获取数据
    getOneData: function() {
        var oId = parseInt(this.data.newsId);
        var signOne = this.data.signWord;
        switch(signOne){
            case "0":
                hotDatas_col.doc(oId).get().then(res => {
                    this.data.oneDetail.push(res.data);
                    this.data.oneDetail.forEach( item => {
                        let a = new Date(item.showTime);
                        item.showTime = a.getFullYear() + '-' + (a.getMonth() + 1) + '-' + a.getDate()
                    })
                    this.setData({
                        oneDetail: this.data.oneDetail
                    })
                  });
                break;
            case "1":
                goods_col.doc(oId).get().then(res => {
                    this.data.oneDetail.push(res.data);
                    this.data.oneDetail.forEach( item => {
                        let a = new Date(item.showTime);
                        item.showTime = a.getFullYear() + '-' + (a.getMonth() + 1) + '-' + a.getDate()
                    })
                    this.setData({
                        oneDetail: this.data.oneDetail
                    })
                  });
                break;
        }
        
    },

    //点击更多跟贴按钮
    longClick: function() {
        wx.navigateTo({
            url: '/pages/follow/follow',
          })
    },

    // 点击收藏
    collectDo: function(e) {
        var self = this;
        let signId = app.globalData.userId;
        let newCode = app.globalData.code;
        if(signId !== "" && newCode !== "") {
            var signTwo = self.data.signWord;
            var newId = e.currentTarget.dataset.videoid;
            var newIndex = e.currentTarget.dataset.index;
            var item = self.data.oneDetail[newIndex];
            item.collected = !item.collected;
    
            if(item.collected === true){
                wx.showToast({
                title: '收藏成功',
                icon: 'success',
                duration: 2000
                })        
            }else{
                wx.showToast({
                title: '取消收藏成功',
                icon: 'success',
                duration: 2000
                })        
            };
    
            switch(signTwo){
                case "0":
                    hotDatas_col.doc(newId).update({
                        data:{
                            collected: item.collected,
                            userId: signId,
                        },
                        success: function(res) {
                            console.log(res)
                        }
                    });
                    self.setData({
                        oneDetail: self.data.oneDetail
                    })
                    break;
                
                case "1":
                    goods_col.doc(newId).update({
                        data:{
                            collected: item.collected,
                            userId: signId,
                        },
                        success: function(res) {
                            console.log(res)
                        }
                    });
                    self.setData({
                        oneDetail: self.data.oneDetail
                    })
                    break;
            };
        }else{
            wx.showToast({
              title: '未登录!',
              icon: "error",
              duration: 2000
            });   
          }
        
    },

    // 分享给微信好友
    onShareAppMessage:function() {
        var self = this;
        return {
            title: self.data.oneDetail.title,
            path: '/pages/details/details?id=' + self.data.newsId + '&sign=' +self.data.signWord,//这里是被分享的人点击进来之后的页面
        }
    },

    // 分享到朋友圈
    onShareTimeline: function(){
        return{
            title: this.data.oneDetail.title, //字符串  自定义标题
            imageUrl:"../../images/news.png"   //图片地址
        }
    },

    // 分享朋友圈按钮
    sharePyq: function() {
        wx.showToast({
          title: '未实现',
          icon: "error",
          duration: 2000
        });  
    },

    // 未登录的分享按钮事件
    noShare: function() {
        wx.showToast({
            title: '未登录!',
            icon: "error",
            duration: 2000
          });  
    },

})